//
//  VisitorView.swift
//  weibo1
//
//  Created by 代亚洲 on 16/4/17.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit
import SnapKit

//import AFNetworking

//定义协议
@objc protocol VisitorViewDelegate :NSObjectProtocol {
    
    func didregisterclickbutton()
    func didloginClickbutton()
}


class VisitorView: UIView {
    //    遵守协议代理
    weak var delegate : VisitorViewDelegate?
    
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        self.backgroundColor = UIColor(white: 237/255.0, alpha: 1)
        
        setupUI()
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //    访客视图内容
    
    func setvistorinfo (imagename : String? , message : String?) {
        if  let imn = imagename , meg = message {
            
            iconView.image = UIImage(named: imn)
            messageLabel.text = meg
        }else{
            
            startanimt()
        }
    }
    
    //    开始动画
    func startanimt() {
        circleView.hidden = false
        //       旋转方式
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        //        旋转角度
        anim.toValue = 2*M_PI
        //       旋转时间
        anim.duration = 10
        //        重复次数
        anim.repeatCount = MAXFLOAT
        //         如果界面切换，或者app退出到后台，默认会把动画给释放掉
        //         设置此属性，解决以上问题f
        anim.removedOnCompletion = false
        //        把动画效果添加到上circleView
        circleView.layer.addAnimation(anim, forKey: nil)
    }
    
    
    //    设置视图
    func setupUI() {
        
        //        1添加控件
        //圆形
        addSubview(circleView)
        //        透明遮罩
        addSubview(maskIconView)
        //        房子
        addSubview(iconView)
        //        消息文本
        addSubview(messageLabel)
        //        注册按钮
        addSubview(registerButton)
        //        登录按钮
        addSubview(loginButton)
        
        // 2. 添加约束 --> 系统原生的约束
        iconView.translatesAutoresizingMaskIntoConstraints = false
        /**
        参数1：要添加约束的对象
        参数2：要添加哪个约束
        参数3：约束与约束之前的关系 一般都用 equal
        参数4：参照约束的对象
        参数5：要约束参照对象的那个约束
        参数6：乘积
        参数7：偏移量
        */
        iconView.snp_makeConstraints { (make) -> Void in
            
            make.center.equalTo(self)
            //            make.centerX.equalTo(self.snp_centerX)
            //            make.centerY.equalTo(self.snp_centerY)
        }
        circleView.snp_makeConstraints { (make) -> Void in
            
            make.centerX.equalTo(iconView.snp_centerX)
            make.centerY.equalTo(iconView.snp_centerY)
            
            //            make.center.equalTo(iconView)
        }
        
        messageLabel.snp_makeConstraints { (make) -> Void in
            
            make.centerX.equalTo(iconView.snp_centerX)
            make.top.equalTo(circleView.snp_bottom).offset(15)
        }
        
        registerButton.snp_makeConstraints { (make) -> Void in
            
            make.leading.equalTo(messageLabel.snp_leading)
            make.top.equalTo(messageLabel.snp_bottom).offset(15)
            make.width.equalTo(100)
            
        }
        
        loginButton.snp_makeConstraints { (make) -> Void in
            
            make.trailing.equalTo(messageLabel.snp_trailing)
            make.top.equalTo(registerButton.snp_top)
            make.width.equalTo(100)
            
        }
        
        maskIconView.snp_makeConstraints { (make) -> Void in
            
            make.centerX.equalTo(circleView.snp_centerX)
            make.centerY.equalTo(circleView.snp_centerY)
        }
        
        
        //        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        //        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))
        //
        // 圆圈
        //        circleView.translatesAutoresizingMaskIntoConstraints = false
        //        addConstraint(NSLayoutConstraint(item: circleView, attribute: NSLayoutAttribute.CenterX, relatedBy: .Equal, toItem: iconView, attribute: .CenterX, multiplier: 1, constant: 0))
        //        addConstraint(NSLayoutConstraint(item: circleView, attribute: NSLayoutAttribute.CenterY, relatedBy: .Equal, toItem: iconView, attribute: .CenterY, multiplier: 1, constant: 0))
        //
        // 消息label
        
        
        //        messageLabel.translatesAutoresizingMaskIntoConstraints = false
        //        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: .Equal, toItem: iconView, attribute: .CenterX, multiplier: 1, constant: 0))
        //        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Top, relatedBy: .Equal, toItem: circleView, attribute: .Bottom, multiplier: 1, constant: 15))
        //
        
        //        registerButton.translatesAutoresizingMaskIntoConstraints = false
        //        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Leading, relatedBy: .Equal, toItem: messageLabel, attribute: .Leading, multiplier: 1, constant: 0))
        //        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Top, relatedBy: .Equal, toItem: messageLabel, attribute: .Bottom, multiplier: 1, constant: 15))
        //        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100))
        //
        // 登录按钮
        
        
        
        //        loginButton.translatesAutoresizingMaskIntoConstraints = false
        //        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Trailing, relatedBy: .Equal, toItem: messageLabel, attribute: .Trailing, multiplier: 1, constant: 0))
        //        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Top, relatedBy: .Equal, toItem: registerButton, attribute: .Top, multiplier: 1, constant: 0))
        //        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100))
        //
        // 透明度
        
        //        maskIconView.translatesAutoresizingMaskIntoConstraints = false
        //        addConstraint(NSLayoutConstraint(item: maskIconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: circleView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        //        addConstraint(NSLayoutConstraint(item: maskIconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: circleView, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))
        
        
    }
    
    //        3加载数据
    private lazy var iconView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    
    private lazy var circleView: UIImageView = {
        
        let iamge = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
        //       隐藏
        iamge.hidden = true
        
        iamge.sizeToFit()
        
        return iamge
    }()
    private lazy var  maskIconView :UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
    private lazy var messageLabel: UILabel = {
        // UILabel(textColor: UIColorxx,  fontSize: 16)
        let label = UILabel(textColor: UIColor.darkGrayColor(), fontSize: 16, preferredMaxLayoutWidth: 215)
        label.text = "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜"
        label.textAlignment = .Center
        
        //        label.textColor = UIColor.darkGrayColor()
        //        label.font = UIFont.systemFontOfSize(16)
        
        // 以下两行代码已抽取到 UILabel 的 extension 的便利构造函数里面去
        //        // 设置多行模式
        //        label.numberOfLines = 0
        //        // 设置最大布局的宽度
        //           label.preferredMaxLayoutWidth = 200
        return label
    }()
    
    // MARK: -  监听方法   代理方法实现
    
    //   @objc private
    func didregisterclickbutton(){
        
        delegate?.didregisterclickbutton()
        
    }
    
    //   @objc private
    func didloginClickbutton(){
        delegate?.didloginClickbutton()
    }
    
    // 注册按钮
    private lazy var registerButton: UIButton = {
        let button = UIButton(textColor: UIColor.orangeColor(), fontSize: 14)
        button.setTitle("注册", forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "common_button_white"), forState: UIControlState.Normal)
        button.addTarget(self, action: "didregisterclickbutton", forControlEvents: UIControlEvents.TouchUpInside)
        return button
    }()
    
    // 登录按钮
    private lazy var loginButton: UIButton = {
        let button = UIButton(textColor: UIColor.darkGrayColor(), fontSize: 14)
        
        button.addTarget(self, action: "didloginClickbutton", forControlEvents: UIControlEvents.TouchUpInside)
        button.setTitle("登录", forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "common_button_white"), forState: UIControlState.Normal)
        
        return button
    }()
    
    
}
